package pl.pelcra.cesar.spider.utils;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.SubnodeConfiguration;
import org.apache.commons.configuration.XMLConfiguration;

/**
 * Class parse and store configuration file like object
 * 
 * @author margielewski
 * 
 */
public class CesarSpiderAppConfiguration {

	private int connectionTimeout;
	private String crawlStorageFolder;
	private String parserClassName;
	private String parserParameters;
	private int politenessDelay = 0;
	private String proxyHost;
	private String proxyPassword;
	private int proxyPort;
	private String proxyUsername;

	private boolean runAttachmentDownloader;
	private boolean runSpider;
	private int socketTimeout;
	private String startSiteUrl;
	private String userAgentString;

	private String webTasksClassName;
	private String webTasksParameters;

	/**
	 * 
	 * @param configFileName
	 *            configuration file name
	 */
	public CesarSpiderAppConfiguration(String configFileName) {

		XMLConfiguration config;
		try {
			config = new XMLConfiguration(configFileName);

			setCrawlStorageFolder(config.getString("crawlStorageFolder"));
			setPolitenessDelay(Integer.valueOf(config
					.getString("politenessDelay")));
			setUserAgentString(config.getString("politenessDelay"));
			setConnectionTimeout(Integer.valueOf(config
					.getString("connectionTimeout")));
			setSocketTimeout(Integer.valueOf(config.getString("socketTimeout")));

			setProxyHost((config.getString("proxyHost")));
			setProxyPassword((config.getString("proxyPassword")));
			setProxyUsername((config.getString("proxyUsername")));
			String sProxyPort = config.getString("proxyPort");

			if (sProxyPort != null && !sProxyPort.isEmpty()) {
				setProxyPort(Integer.valueOf(sProxyPort));
			}

			SubnodeConfiguration scPageProperties = config
					.configurationAt("pageProperties");
			SubnodeConfiguration scWebTask = scPageProperties
					.configurationAt("webTask");
			setWebTasksClassName(scWebTask.getString("className"));
			setWebTasksParameters(scWebTask.getString("parameters"));

			SubnodeConfiguration scPageParser = scPageProperties
					.configurationAt("pageParser");
			setParserClassName(scPageParser.getString("className"));
			setParserParameters(scPageParser.getString("parameters"));

			setStartSiteUrl(config.getString("startSiteUrl"));

			setRunAttachmentDownloader(Boolean.parseBoolean(config
					.getString("runAttachmentDownloader")));
			setRunSpider(Boolean.parseBoolean(config.getString("runSpider")));

		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return return connection timeout
	 */
	public int getConnectionTimeout() {
		return this.connectionTimeout;
	}

	/**
	 * 
	 * @return return crawl storage folder for HttpClient class
	 */
	public String getCrawlStorageFolder() {
		return this.crawlStorageFolder;
	}

	/**
	 * 
	 * @return return parser class name
	 */
	public String getParserClassName() {
		return this.parserClassName;
	}

	/**
	 * 
	 * @return return parser parameters
	 */
	public String getParserParameters() {
		return this.parserParameters;
	}

	/**
	 * 
	 * @return return politeness delay in miliseconds
	 */
	public int getPolitenessDelay() {
		return this.politenessDelay;
	}

	/**
	 * 
	 * @return return host name of proxy
	 */
	public String getProxyHost() {
		return this.proxyHost;
	}

	/**
	 * 
	 * @return return proxy password
	 */
	public String getProxyPassword() {
		return this.proxyPassword;
	}

	/**
	 * 
	 * @return return proxy port
	 */
	public int getProxyPort() {
		return this.proxyPort;
	}

	/**
	 * 
	 * @return return proxy user name
	 */
	public String getProxyUsername() {
		return this.proxyUsername;
	}

	/**
	 * 
	 * @return return socket timeout
	 */
	public int getSocketTimeout() {
		return this.socketTimeout;
	}

	/**
	 * 
	 * @return return url - start url to crawling page
	 */
	public String getStartSiteUrl() {
		return this.startSiteUrl;
	}

	/**
	 * 
	 * @return return user agent string of crawler
	 */
	public String getUserAgentString() {
		return this.userAgentString;
	}

	/**
	 * 
	 * @return return web task class name
	 */
	public String getWebTasksClassName() {
		return this.webTasksClassName;
	}

	/**
	 * 
	 * @return return web task class parameters
	 */
	public String getWebTasksParameters() {
		return this.webTasksParameters;
	}

	/**
	 * 
	 * @return return boolean value, true to download attachment, false to not
	 *         download attachment
	 */
	public boolean isRunAttachmentDownloader() {
		return this.runAttachmentDownloader;
	}

	/**
	 * 
	 * @return return boolean value, true to run thread spider otherwise do not
	 *         start spider
	 */
	public boolean isRunSpider() {
		return this.runSpider;
	}

	private void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	private void setCrawlStorageFolder(String crawlStorageFolder) {
		this.crawlStorageFolder = crawlStorageFolder;
	}

	private void setParserClassName(String basePageParserClassName) {
		this.parserClassName = basePageParserClassName;
	}

	private void setParserParameters(String parserParameters) {
		this.parserParameters = parserParameters;
	}

	private void setPolitenessDelay(int politenessDelay) {
		this.politenessDelay = politenessDelay;
	}

	private void setProxyHost(String proxyHost) {
		this.proxyHost = proxyHost;
	}

	private void setProxyPassword(String proxyPassword) {
		this.proxyPassword = proxyPassword;
	}

	private void setProxyPort(int proxyPort) {
		this.proxyPort = proxyPort;
	}

	private void setProxyUsername(String proxyUsername) {
		this.proxyUsername = proxyUsername;
	}

	private void setRunAttachmentDownloader(boolean runAttachmentDownloader) {
		this.runAttachmentDownloader = runAttachmentDownloader;
	}

	private void setRunSpider(boolean runSpider) {
		this.runSpider = runSpider;
	}

	private void setSocketTimeout(int socketTimeout) {
		this.socketTimeout = socketTimeout;
	}

	private void setStartSiteUrl(String startSiteUrl) {
		this.startSiteUrl = startSiteUrl;
	}

	private void setUserAgentString(String userAgentString) {
		this.userAgentString = userAgentString;
	}

	private void setWebTasksClassName(String basePageWebTasksClassName) {
		this.webTasksClassName = basePageWebTasksClassName;
	}

	private void setWebTasksParameters(String webTasksParameters) {
		this.webTasksParameters = webTasksParameters;
	}
}
